(function(){
	var gridOptions = {};
	var autoForm = {
		loadMoal:function (params){
			$("#"+params.loadModelAreaId).html("");
			if(params.isAdd == false){
				var data = $(this).wgrid("getTrData",{
					tbodyId:params.tbodyId
				});
				if(data.length != 1){
					alert("必须选中一条数据!");
					return;
				}
			}
				$("#"+params.loadModelAreaId).html("");
				$("#"+params.loadModelAreaId).load(params.modalPageUrl,{},function(){
					$("#"+params.momdalId).on("show.bs.modal",function(event){
						if(params.isAdd == false){
							var json = $("#"+gridOptions.tableNameId).data("formData");
							console.log(json);
							var formJson = jQuery.parseJSON(json.table_info);
							for(var i = 0;i < formJson.length;i++){
								var type = formJson[i].type;
								if(type.indexOf("file") != -1){
									data[0]["uploadFilePathId"+"_"+formJson[i].sort] = formJson[i].column;
								}
							}
							setFormData(params.formBodyId,data[0]);
						}
					});
					$("#"+params.momdalId).modal("show");
					
					params.success(this);
				});
			},

		loadGrid:function (event,options){
			console.log("============loadGrid===================");
			console.log(options);
				if(typeof(options.data)=="undefined"){
					options.data={};
				}
				options.data.start=1;
				//var url = ;
				$("#"+options.gridTableId).wgrid({
					table_title:options.table_title,
					tbodyId:options.tbodyId,
					pagination:options.pagination,
					url:options.loadGridDataUrl,
					checkbox:options.checkbox,
					checkboxId: options.checkboxId,
					data:options.data,
					imgColunm: options.imgColunm,
					success:function(){
					//	changeUserState();
						options.backFunction();
						
					}
				});
			},
		loadGridTitle:function (data,obj,options){
				//var url =;
				$.post(options.loadGridTitleUrl,data,function(dataJson){
					$("#"+options.table_title).html("");
					$("#"+options.table_title).append('<th style="width:100px; ">编号</th>');
					$("#"+options.table_title).append('<th  width="50" fname="companyNameCheckBox"><input id="add_checkbox" type="checkbox"></th>');
					var jsonT = jQuery.parseJSON(dataJson);
					var json = jQuery.parseJSON(jsonT.table_info);
					for(var i = 0;i < json.length;i++){
						$("#"+options.table_title).append('<th fname="'+json[i].column+'">'+json[i].name+'</th>');
					}
					$("#"+options.table_title).append('<th fname="showTime">创建时间</th>');
					
					options.name = $(obj).attr("table_name");
					options.data.start = 1;
					options.data.limit = 20;
					$("#"+options.tableNameId).val($(obj).attr("table_name"));
					$("#"+options.tableNameId).data("formData",jsonT);
					$("#"+options.tableNameId).data("tableNameData",$(obj).attr("table_name"));

					autoForm.loadSelectCondition(options);
					autoForm.loadGrid(event,options);
					
					$("#"+options.searchId).on("click",function(){
						autoForm.searchList(options);
					});
				});
			},
		loadSelectCondition: function(options){
			var json = $("#"+options.tableNameId).data("formData");
			var formJson = jQuery.parseJSON(json.table_info);
			for(var i = 0;i < formJson.length;i++){
				if(formJson[i].hasOwnProperty("condition")){
					var type = formJson[i].type;
					var _parentDiv = document.createElement("div");
					
					_parentDiv.setAttribute("class","mp");
					_parentDiv.setAttribute("style","float: left;margin-left: 10px;height:40px;line-height: 40px;padding-top: 3px;");
					
					
					if(type == 'text' || type == 'textArea' ){
						var _inputIds = $("#"+options.searchCondition).find("input");
						if(_inputIds.length <= 0){
							var _input = document.createElement("input");
							_input.setAttribute("id",formJson[i].column);
							_input.setAttribute("name",formJson[i].column);
							_input.setAttribute("type","text");
							_input.setAttribute("style","width: 150px;height: 37px;");
							_input.setAttribute("value",formJson[i].val);
							$(_input).attr("colunm","");
							$(_parentDiv).append(_input);
						}else{
							if(typeof(_inputIds.attr("colunm")) == "undefined"){
								_inputIds.attr("colunm",formJson[i].column);
							}else{
								_inputIds.attr("colunm",_inputIds.attr("colunm")+","+formJson[i].column);
							}
						}
						
					}
					
					if(type.indexOf("select") != -1 || type.indexOf('checkbox') != -1
								|| type.indexOf('radio') != -1){
						var _select = document.createElement("select");
						_select.setAttribute("id",formJson[i].column);
						_select.setAttribute("name",formJson[i].column);
						_select.setAttribute("style","width: 150px;height: 37px;");
						
						var vals = new Array();
						if(formJson[i].val.indexOf(",") != -1){
							vals = formJson[i].val.split(",");
						}else{
							vals[0] = formJson[i].val;
						}
						for(var j = 0;j < vals.length-1;j++){
							var _options = document.createElement("option");
							if(vals[j].indexOf(":") != -1 && !jQuery.isEmptyObject(vals[j])){
								_options.setAttribute("value",vals[j].split(":")[1]);
								_options.textContent = vals[j].split(":")[0];
							}else{
								_options.setAttribute("value",vals[j]);
								_options.textContent = vals[j];
							}
							_select.appendChild(_options);
						}
						$(_parentDiv).append(_select);
					}
					$("#"+options.searchCondition).prepend(_parentDiv);
				}
			}
		},
		loadFormHtmlData: function(options){
			
			$("#"+options.formBodyId).html("");
			
			var json = $("#"+options.tableNameId).data("formData");
			var formJson = jQuery.parseJSON(json.table_info);
			var _tableIdInput = document.createElement("input");
			
				_tableIdInput.setAttribute("id","table_id");
				_tableIdInput.setAttribute("name","table_id");
				_tableIdInput.setAttribute("type","hidden");
				
				$("#"+options.formBodyId).append(_tableIdInput);
				
			for(var i = 0;i < formJson.length;i++){
				
				var type = formJson[i].type;
				var _parentDiv = document.createElement("div");
				var _childSpan = document.createElement("span");
				var _childDiv = document.createElement("div");
				
				_parentDiv.setAttribute("class","input-group");
				_parentDiv.setAttribute("style","margin-bottom:5px;");
				_childSpan.setAttribute("class","input-group-addon");
				_childSpan.setAttribute("style","width: 100px;");
				_childSpan.textContent = formJson[i].name;
				_childDiv.setAttribute("class","dropdown");
				_childDiv.setAttribute("style","height: 37px;width: 300px;");
				
				
				if(type == 'text'){
					var _input = document.createElement("input");
					_input.setAttribute("id",formJson[i].column);
					_input.setAttribute("name",formJson[i].column);
					_input.setAttribute("type","text");
					_input.setAttribute("style","width: 300px;height: 37px;");
					_input.setAttribute("value",formJson[i].val);
					_childDiv.appendChild(_input);
				}
				if(type == 'file'){
					
					var _file = document.createElement("input");
					var _form = document.createElement("form");
					var _iframe = document.createElement("iframe");
					var _hidden = document.createElement("input");
					var _input = document.createElement("input");
					
					_input.setAttribute("id","uploadFilePathId"+"_"+formJson[i].sort);
					_input.setAttribute("name","uploadFilePathId");
					_input.setAttribute("type","hidden");
					$(_input).val(formJson[i].column);
				
					
					_iframe.setAttribute("name",formJson[i].column+"_iframe");
					_iframe.setAttribute("style","display:none");
					
					_form.setAttribute("action","/DynamicForm/dynamicForm/dynamicFormAction.do?method=uploadFile");
					_form.setAttribute("enctype","multipart/form-data");
					_form.setAttribute("method","post");
					_form.setAttribute("target",formJson[i].column+"_iframe");
					
					_file.setAttribute("type","file");
					_file.setAttribute("name","file");
					_file.setAttribute("class","file");
					
					_hidden.setAttribute("type","hidden");
					_hidden.setAttribute("name",formJson[i].column);
					_hidden.setAttribute("id",formJson[i].column);
					
					_form.appendChild(_file);
					_form.appendChild(_input);
					_childDiv.appendChild(_form);
					
					$("#"+options.formBodyId).append(_hidden);
					$("#"+options.formBodyId).append(_iframe);
					
					$(_file).on("change",function(){
						var s = window.confirm("你确认要上传文件吗?");
						if(s == false){
							return;
						}
						$(this).parent().submit();
					});
				}
				if(type == 'textArea'){
					var _textArea = document.createElement("textArea");
					_textArea.setAttribute("id",formJson[i].column);
					_textArea.setAttribute("name",formJson[i].column);
					_textArea.setAttribute("style","width: 300px;height: 37px;");
					_textArea.setAttribute("value",formJson[i].val);
					_childDiv.appendChild(_textArea);
				}
				
				if(type.indexOf("select") != -1){
					var _select = document.createElement("select");
					_select.setAttribute("id",formJson[i].column);
					_select.setAttribute("name",formJson[i].column);
					_select.setAttribute("style","width: 300px;height: 37px;");
					if(type.indexOf("multiple") != -1){
						_select.setAttribute("multiple","multiple");
					}
					var vals = new Array();
					if(formJson[i].val.indexOf(",") != -1){
						vals = formJson[i].val.split(",");
					}else{
						vals[0] = formJson[i].val;
					}
					for(var j = 0;j < vals.length-1;j++){
						var _options = document.createElement("option");
						if(vals[j].indexOf(":") != -1 && !jQuery.isEmptyObject(vals[j])){
							_options.setAttribute("value",vals[j].split(":")[1]);
							_options.textContent = vals[j].split(":")[0];
						}else{
							_options.setAttribute("value",vals[j]);
							_options.textContent = vals[j];
						}
						_select.appendChild(_options);
					}
					_childDiv.appendChild(_select);
				}
				
				if(type.indexOf('checkbox') != -1){
					var arr = new Array();
					if(formJson[i].val.indexOf(",") != -1){
						arr = formJson[i].val.split(",");
					}else{
						arr[0] = formJson[i].val;
					}
					for(var j = 0;j < arr.length - 1;j++){
						var _input = document.createElement("input");
						//_input.setAttribute("id",formJson[i].column);
						_input.setAttribute("name",formJson[i].column);
						_input.setAttribute("type","checkbox")
						var textNode;
						if(arr[j].indexOf(":") != -1){
							_input.setAttribute("value",arr[j].split(":")[1]);
							textNode = document.createTextNode(arr[j].split(":")[0]); 
						}else{
							_input.setAttribute("value",arr[j].split(":"));
							 textNode = document.createTextNode(arr[j].split(":")); 
						}
						_input.setAttribute("style","margin-left:10px;");
						_childDiv.appendChild(_input);
						_childDiv.appendChild(textNode);
						
						if(type.indexOf("inline") == -1){
							var _br = document.createElement("br");
							_childDiv.appendChild(_br);
						}
					}
				}
				if(type.indexOf('radio') != -1){
					var arr = new Array();
					if(formJson[i].val.indexOf(",") != -1){
						arr = formJson[i].val.split(",");
					}else{
						arr[0] = formJson[i].val;
					}
					for(var j = 0;j < arr.length - 1;j++){
						var _input = document.createElement("input");
						//_input.setAttribute("id",formJson[i].column);
						_input.setAttribute("name",formJson[i].column);
						_input.setAttribute("type","radio")
						var textNode;
						if(arr[j].indexOf(":") != -1){
							_input.setAttribute("value",arr[j].split(":")[1]);
							textNode = document.createTextNode(arr[j].split(":")[0]); 
						}else{
							_input.setAttribute("value",arr[j].split(":"));
							 textNode = document.createTextNode(arr[j].split(":")); 
						}
						_input.setAttribute("style","margin-left:10px;");
						_childDiv.appendChild(_input);
						_childDiv.appendChild(textNode);
						
						if(type.indexOf("inline") == -1){
							var _br = document.createElement("br");
							_childDiv.appendChild(_br);
						}
					}
				}
				_parentDiv.appendChild(_childSpan);
				_parentDiv.appendChild(_childDiv);
				
				$("#"+options.formBodyId).append(_parentDiv);
				options.backFunction(this);
			}
		},
		addOrUpdate: function (options){
			var params = {};
			var s = $("#"+options.addBodyId).hzValidate("init");
			if(s == true){
				 var json = OAajaxSubmit(options.addBodyId);
				 params = {
						 "name": $("#"+options.tableNameId).data("tableNameData"),
						 "jsonStr": JSON.stringify(json)
				 }
				 $.post(options.url,params,function(data){
					 var reJSon = jQuery.parseJSON(data);
					 console.log("================addOrUpdate==============");
					 console.log(reJSon);
					 if(reJSon.status == 1){
						 	gridOptions.name = $("#"+options.tableNameId).data("tableNameData");
						 	gridOptions.data.start = 1;
							gridOptions.data.limit = 20;
						 $("#"+options.addOrUpdateModal).modal("hide");
						 autoForm.loadGrid(event,gridOptions);
					 }
					 alert(reJSon.msg);
					 options.backFunction(this);
				 });
			}
			
		},
		deleteModel:function(options){
			var data = $('#table_role').wgrid("getTrData",{
				tbodyId: options.tbodyId
			});
			if(data.length != 1){
				alert("请选择一条数据");
				return;
			}
			var s = window.confirm("是否删除数据?");
			if(s == false){
				return;
			}
			var params = {
					"name": $("#"+options.tableNameId).data("tableNameData"),
					"jsonStr":JSON.stringify({"table_id":data[0].table_id})	
			};
			$.post(options.url,params,function(data){
				var json = jQuery.parseJSON(data);
				if(json.status == 1){
					gridOptions.name = $("#"+options.tableNameId).data("tableNameData");
				 	gridOptions.data.start = 1;
					gridOptions.data.limit = 20;
					autoForm.loadGrid(event,gridOptions);
				}
				alert(json.msg);
				options.backFunction(this);
			});
		},
		searchList: function(options){
			var params = OAajaxSubmit(options.searchCondition);
			console.log("============searchList=================");
			console.log(params);
			var _inputIds = $("#"+options.searchCondition).find("input");
			if(_inputIds.length > 0){
				var colunm = _inputIds.eq(0).attr("colunm"); 
				if(colunm.indexOf(",") != -1){
					var arr = colunm.split(",");
					for(var i = 0;i < arr.length;i++){
						console.log(arr[i]);
						params[arr[i]] = _inputIds.eq(0).val();
					}
				}else{
					if(!jQuery.isEmptyObject(colunm)){
						params[colunm] = _inputIds.eq(0).val();
					}
				}
			}
			console.log(params);
			
			gridOptions.name = $("#"+options.tableNameId).data("tableNameData");
		 	gridOptions.data.start = 1;
			gridOptions.data.limit = 20;
			gridOptions.data.jsonStr = JSON.stringify(params);
			console.log(gridOptions);
			autoForm.loadGrid(event,gridOptions);
		}
	};
	$.fn.extend({
		loadGridTitle: function(obj,option){
			var options = $.extend({
				gridTableId: "gridTableId",
				table_title:"title_id",
				tbodyId:"info_tbody_id",
				pagination:"pagination",
				loadGridDataUrl:"",
				loadGridTitleUrl:"",
				checkbox:true,
				checkboxId: "add_checkbox",
				data:{},
				tableNameId: "tableNameId",
				imgColunm: "",
				backFunction:function(obj){
					
				}
			},option);
			gridOptions = options; 
			autoForm.loadGridTitle({"table_en_name":$(obj).attr("table_name")},obj,options);
		},
		loadGrid:function(option){
			var options = $.extend({
				gridTableId: "gridTableId",
				table_title:"title_id",
				tbodyId:"info_tbody_id",
				pagination:"pagination",
				loadGridDataUrl:"",
				loadGridTitleUrl:"",
				checkbox:true,
				checkboxId: "add_checkbox",
				data:{},
				tableNameId: "tableNameId",
				backFunction:function(obj){
					
				}
			},option);
			autoForm.loadGrid(event,options);
		},
		loadMoal:function(options){
			var params = $.extend({
				momdalId: "modelId",
				loadModelAreaId: "loadModelAreaId",
				modalPageUrl: "modalPageUrl",
				formBodyId: "",
				isAdd: true,
				tbodyId: "info_tbody_id",
				success:function(obj){
					
				}
			},options);
			autoForm.loadMoal(params);
		},
		loadFormHtmlData: function(options){
			var params = $.extend({
				formBodyId: '',
				tableNameId: '',
				backFunction: function(obj){
					
				}
			},options);
			autoForm.loadFormHtmlData(params);
		},
		addOrUpdate: function(options){
			var params = $.extend({
				url: "",
				addBodyId: "",
				addOrUpdateModal: "",
				tableNameId: "",
				backFunction: function(obj){
					
				}
			},options);
			autoForm.addOrUpdate(params);
		},
		deleteModel:function(options){
			var params = $.extend({
					tbodyId: "",
					tableNameId: "",
					url: "",
					backFunction: function(obj){
						
					}
			},options);
			autoForm.deleteModel(params);
		}
	});
})();